const sequelize = require('../db')
const { DataTypes } = require('sequelize')
const Article = require('./Article')
const Column = require('./Column')

const ArticleColumn = sequelize.define(
  'ArticleColumn',
  {
    ArticleId: {
      type: DataTypes.INTEGER,
      references: {
        model: Article,
        key: 'id'
      }
    },
    ColumnId: {
      type: DataTypes.INTEGER,
      references: {
        model: Column,
        key: 'id'
      }
    }
  },
  {
    freezeTableName: true,
    createdAt: false,
    deletedAt: false
  }
)

Article.belongsToMany(Column, {
  through: {
    model: ArticleColumn,
    unique: false
  },
  foreignKey: 'ArticleId',
  constraints: false
})
Column.belongsToMany(Article, {
  through: {
    model: ArticleColumn,
    unique: false
  },
  foreignKey: 'ColumnId',
  constraints: false
})

// ArticleColumn.belongsTo(Article)
// ArticleColumn.belongsTo(Column)
// Article.hasMany(ArticleColumn)
// Column.hasMany(ArticleColumn)

module.exports = ArticleColumn
